<template>
  <div class="box">
    <div class="title">相似视频</div>
    <div class="content">
      <div class="item" v-for="item in resV_" :key="item.vid">
        <div class="left">
          <div class="count">
            <i class="el-icon-video-play"></i>
            {{
                item.playTime >= 10000
                  ? (item.playTime / 10000).toFixed(0) + "万"
                  : item.playTime
            }}
          </div>
          <div class="time">
            {{ Math.floor(item.durationms / 1000) | timeFormat }}
          </div>
          <el-image :src="item.coverUrl" fit="contain" @click="play(item.vid)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image src="https://s4.music.126.net/style/web2/img/default/default_album.jpg" @click="play(item.vid)"
                style="width: 200px; height: 100px" lazy></el-image>
            </div>
          </el-image>
        </div>
        <div class="right">
          <div>{{ item.title }}</div>
          <div v-for="item2 in item.creator" :key="item2.userId">
            <div>
              {{
                  item.creator.lenght > 1 ? item2.userName + "/" : item2.userName
              }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  computed: {
    ...mapState(['resV']),
  },
  mounted() {
    if (this.resV !== null) {
      this.resV_ = this.resV.resV;
    }
  },

  watch: {
    resV(newVal, oldVal) {
      this.resV_ = newVal.resV;
    },
  },

  data() {
    return {
      loading: false,
      resV_: [],
    };
  },
  methods: {
    ...mapActions(['saveVurl', 'saveVbes', 'saveResv']),
    async play(e) {
      this.$api.videoUrl({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveVurl({ vUrl: ret.urls[0].url });
        this.getResMv(e);
        this.getItem(e);
      });
    },

    // 获取相似MV
    async getResMv(e) {
      this.$api.allvideo({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveResv({ resV: ret.data });
      });
    },

    // 获取视频基础信息
    async getItem(e) {
      this.loading = true;
      this.$api.videoDetail({ id: e, timestamp: this.$utils.timestamp(), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveVbes({ vBes: ret.data });
        this.$router.push(`/videoPage?id=${e}`);
        this.loading = false;
      });
    },
  },
};
</script>

<style scoped>
.box {
  margin-left: 15px;
  display: flex;
  flex-direction: column;
}

.title {
  font-size: 20px;
  margin-bottom: 32px;
}

.item {
  display: flex;
  margin-bottom: 15px;
}

.content {
  width: 100%;
}

.left {
  display: flex;
  flex-direction: column;
  position: relative;
}

.el-image {
  width: 200px;
  height: 100px;
}

.right {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.count {
  position: absolute;
  top: 0;
  right: 10px;
  z-index: 10;
  color: #fff;
  font-weight: 700;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}

.time {
  position: absolute;
  bottom: 0;
  right: 10px;
  z-index: 10;
  color: #fff;
  font-weight: 700;
}
</style>
